#include "widget.h"
#include "ui_widget.h"
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //添加元素
//    ui->listWidget->addItem("C++");
//    ui->listWidget->addItem("Java");
//    ui->listWidget->addItem("Python");

    ui->listWidget->addItem(new QListWidgetItem("C++"));
    ui->listWidget->addItem(new QListWidgetItem("Java"));
    ui->listWidget->addItem(new QListWidgetItem("Python"));
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_insert_clicked()
{
    const QString& text = ui->lineEdit->text();
    ui->listWidget->addItem(text);
}

void Widget::on_pushButton_delete_clicked()
{
    //获取当前选中的行号
    int row = ui->listWidget->currentRow();
    if(row < 0)
    {
        return;
    }
    //按照行号来删除元素
    ui->listWidget->takeItem(row);
}

void Widget::on_listWidget_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
{
    if(current != nullptr)
    {
        qDebug() << "当前选中的元素为: " << current->text();
    }
    if(previous != nullptr)
    {
        qDebug() << "上次选中的元素为: " << previous->text();
    }
}
